package com.jiamy.config;

import com.jiamy.job.MyJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail jobDetail() {
        return JobBuilder.newJob(MyJob.class)
                .storeDurably()  //持久化
                .withIdentity("job1", "group1")  //唯一标识
                .usingJobData("count", 1)  //（共享）数据初始化
                .build();
    }

    @Bean
    public Trigger trigger() {
        // cron表达式
        String cronExpression = "0/5 * * * * ? *";  //每隔5秒执行一次
        return TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")  //唯一标识
                .forJob(jobDetail())
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))  //时间规则：cron表达式
                .build();
    }
}
